<?php
/**
 * Handles and manages an company and its entity in database
 * - based on CRUD
 *
 */
class CompanyModel extends Model {

    public function createCompany(){
    }

    public function readCompany(){
    }

    public function updateCompany(){
    }

    public function deleteCompany(){
    }


    /*
     *  Relations
     */
    
    /* company_page */
    public function createCompanyPage(){
        
    }

    public function readCompanyPage($kplID){

        $result = $this->db->query('
            SELECT *
            FROM klimat_comp
            WHERE id = ?
            ', 'i', $kplID);

        if(count($result) > 0)
            return $result;
        else
            return false;
    }
    
    public function updateCompanyPage($id){
    }

    public function deleteCompanyPage($id){
    }

    public function listCompanyPage($adType){
        $result = $this->db->query('
            SELECT klimat_comp.name, klimat_comp.id as id
            FROM klimat_comp
            WHERE klimat_comp.id >= ?
            ', 'i', $adType);

        if(count($result) > 0)
            return $result;
        else
            return false;
    }


    /* company_page_offer */
    public function readCompanyPageOffer($id){
        $result = $this->db->query('
            SELECT co.title, co.content, co.added
            FROM company_offer co
            JOIN company_has_offer cho ON cho.company_offer_id = co.company_offer_id
            WHERE cho.company_page_id = ?
            ', 'i', $id);

        if(count($result) > 0)
            return $result;
        else
            return false;
    }


    public function searchCompanyNews($searchStr){
        $result = $this->db->query("
            SELECT *
            FROM news
            WHERE title LIKE CONCAT('%', ?, '%')
            OR text LIKE CONCAT('%', ?, '%')
            OR text2 LIKE CONCAT('%', ?, '%')
            ", 'sss', $searchStr, $searchStr, $searchStr);

        if(count($result) > 0)
            return $result;
        else
            return false;
    }


    public function searchCompanyVideos($searchStr){
        $result = $this->db->query("
            SELECT *
            FROM videos
            WHERE title LIKE CONCAT('%', ?, '%')
            OR description LIKE CONCAT('%', ?, '%')
            OR description2 LIKE CONCAT('%', ?, '%')
            ", 'sss', $searchStr, $searchStr, $searchStr);

        if(count($result) > 0)
            return $result;
        else
            return false;
    }

    public function searchCompanyAds($searchStr){
        $result = $this->db->query("
            SELECT name, text
            FROM klimat_comp
            WHERE name = ?", 's', $searchStr);

        if(count($result) > 0)
            return $result;
        else
            return false;
    }
}
?>
